import * as types from './types'

export const state = {
  normalRepayment: {
    repays: [],
    additions: {},
    loading: false,
    conditions: {
      mobile: ''
    },
    pageSize: 10,
    total: 0,
    curPage: 1
  }
}

export const getters = {
  normalRepayment_repays: state => state.normalRepayment.repays,
  normalRepayment_loading: state => state.normalRepayment.loading,
  normalRepayment_conditions: state => state.normalRepayment.conditions,
  normalRepayment_checkedTime: state => state.normalRepayment.additions.time,
  normalRepayment_total: state => state.normalRepayment.total,
  normalRepayment_curPage: state => state.normalRepayment.curPage,
  normalRepayment_pageSize: state => state.normalRepayment.pageSize
}

export const actions = {
  getNormalRepays({
    commit
  }, payload) {
    commit(types.GET_NORMAL_REPAYS, payload);
  },
  confirmNormalRepay({
    commit
  }, payload) {
    commit(types.CONFIRM_NORMAL_REPAY, payload);
  }
}

export const mutations = {
  /**
   * 获取还款详情
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     page
   *   } 
   */
  [types.GET_NORMAL_REPAYS](state, {
    vm,
    page
  }) {
    state.normalRepayment.loading = true;
    const params = {
      limit: state.normalRepayment.pageSize,
      mobile: state.normalRepayment.conditions.mobile
    };
    params.start = page ? page : state.normalRepayment.curPage;

    vm.$ajax.get('/repay/showTodayRepayDetl', {
        params: params
      })
      .then(res => {
        let data = res.data;
        if (data != null && data.status == vm.$appConfig.success) {
          state.normalRepayment.repays = data.list;
          state.normalRepayment.additions = data.additions;
          state.normalRepayment.total = data.total;
          state.normalRepayment.pageSize = data.pageSize;
          state.normalRepayment.curPage = data.curPage;
          state.normalRepayment.loading = false;
        } else {
          state.normalRepayment.repays = [];
          state.normalRepayment.loading = false;
        }
      })
      .catch(err => {
        state.normalRepayment.repays = [];
        state.normalRepayment.loading = false;

        vm.$notify.error({
          title: '失败',
          message: '获取还款详情列表失败'
        });
        console.error(err);
      });
  },
  /**
   * 确认本期还款
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     row
   *   } 
   */
  [types.CONFIRM_NORMAL_REPAY](state, {
    vm,
    row,
    cb
  }) {
    vm.$confirm(`是否审核【${row.loan_name}】标的的本期还款?`, '提示', {
        type: 'info'
      })
      .then(() => {
        vm.$ajax.post('/repay/repayThisPeriod', vm.$postParams(row))
          .then(res => {
            let data = res.data;
            if (data && data.status == vm.$appConfig.success) {
              vm.$notify({
                title: '提示',
                message: data.msg,
                type: 'success'
              });
              if (cb) {
                cb();
              }
            }
          })
          .catch(err => {
            vm.$notify.error({
              title: '提示',
              message: '发生了一个错误'
            });
            console.error(err);
          });
      })
      .catch(() => {});
  }
}